<?php
require_once './includes/Start.php';

$serviceId = "";
$repoId = "";
$path = "";
$cmd = "";
$cmsg = "";
$bwstype = "status";

if( isset( $_REQUEST['serviceid'] ) ) { $serviceId = $_REQUEST['serviceid']; }
if( isset( $_REQUEST['repoid']    ) ) { $repoId    = $_REQUEST['repoid'];    }
if( isset( $_REQUEST['path']      ) ) { $path      = $_REQUEST['path'];      }
if( isset( $_REQUEST['cmd']       ) ) { $cmd       = $_REQUEST['cmd'];   }
if( isset( $_REQUEST['cmsg']      ) ) { $cmsg      = $_REQUEST['cmsg'];      }

if( empty( $path ) ) 
    $path = "./";

if( empty( $serviceId ) or empty( $repoId ) ) {
	$message = "파라메터오류입니다.";

    include 'template/svn_execute.php';
    exit;
}

$repo = $dgConfig->getRepoPath($serviceId, $repoId);
$repoPathBase = (string)$repo;

$svn = new SVNLook($repoPathBase, $_COOKIE["uid"], $_COOKIE["passwd"]);

switch( $cmd ) {
	case "commit";
		if( isset( $_POST['justcommit'] ) ) {
			$message .= svnCommit($svn, $path, $cmsg);
			if( $message == "" ) {
				$message = "커밋할만 한게 없습니다.";
			}
		}
		else {
			if( sizeof( $_POST['check'] ) == 0 ) {
				$message = "커밋할 대상이 하나도 선택되지 않았습니다. ";
			}
            else {
			    $dataAll="";
    			foreach($_POST['check'] as $commitData) {
    				list($status, $data) = split(":", $commitData);

    				if( "unversioned" == $status ) {
    					echo "add command :$data";
    					$svn->executeSvnCommand("add", $data, '', '');
    				}
    				$dataAll .= $data." ";
    			}
    			$message .= svnCommit($svn, $dataAll, $cmsg);
            }
		}
	break;
	case "update";
	    $message = $svn->executeSvnCommand('update', $path, '','');
	break;
}

include 'template/svn_execute.php';
?>

<?
#-----------------------------------------------------------------------------------
# SVN Commit
#-----------------------------------------------------------------------------------
function svnCommit($svn, $path, $cmsg) {
	global $dgTempPath;
	$cmsgTempFile = tempnam( @$dgTempPath, "commit_" ); 
	if( $cmsgTempFile == FALSE) {
		return "commit message temprary file creation fail!!...";
		die();
	}

	$handle = fopen($cmsgTempFile, "w");
	fwrite($handle, $cmsg);
	fclose($handle);
	$repoOut = $svn->executeSvnCommand('commit', $path, '', $cmsgTempFile);
	unlink($cmsgTempFile);
	return $repoOut;
}
?>

